package com.alibaba.dingtalk.openapi.helper;

import com.alibaba.dingtalk.openapi.exception.OApiException;
import com.alibaba.dingtalk.openapi.utils.HttpHelper;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.saas.enums.mini.DingBundleRobotMsgKeyEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 应用模块名称<p>
 * 代码描述<p>
 * Company: 逍邦网络科技有限公司<p>
 *
 * @author chenshan
 * @version v1.0
 * @since 2020/9/28 2:50 下午
 */
@Component
public class GroupRobotHelper {
    private static final Logger LOG = LoggerFactory.getLogger(GroupRobotHelper.class);
    @Resource
    private AuthHelper authHelper;
    
    /**
     * 机器人配置:
     * 配置一
     * {"msgtype":"actionCard","msgNoCopy":"1","actionCard":{"btnOrientation":"0","btns":[{"actionURL":"#actionURL#","title":"#actionTitle#"}],"text":"#text# \n\n > 来自销帮帮","title":"#title#"}}
     * msg_key:xbongbong_actionCard_1
     *  msgParam样例如下: {
     * "#actionURL#" : "xxxxxxx",
     * "#actionTitle#" : "xxxxxxx",
     * "#text#" : "xxxxxx",
     * "#title#" : "xxxxxx"
     * }
     *
     * 配置二
     * {"msgtype":"actionCard","msgNoCopy":"1","actionCard":{"btnOrientation":"0","btns":[{"actionURL":"#actionURL1#","title":"#actionTitle1#"},{"actionURL":"#actionURL2#","title":"#actionTitle2#"}],"text":"#text# \n\n > 来自销帮帮","title":"#title#"}}
     * msg_key:xbongbong_actionCard_2
     *  msgParam样例如下: {
     * "#actionURL1#" : "xxxxxxx",
     * "#actionTitle1#" : "xxxxxxx",
     * "#actionURL2#" : "xxxxxxx",
     * "#actionTitle2#" : "xxxxxxx",
     * "#text#" : "xxxxxx",
     * "#title#" : "xxxxxx"
     * }
     *
     * 对方的接口参数为
     * msg_key,msg_param,open_conversation_id,receiver_user_ids,at_user_ids
     * 接口地址：https://oapi.dingtalk.com/topapi/robot/org/intelligent/message/send?access_token=access_token
     *
     * 如果想要传图片格式如下处理：![screenshot](https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png)
     *
     * msgKeyEnum 消息类型
     * corpid 公司编号
     * userName 用户名称
     * dataId 数据编号
     * openConversationId 群编号
     * text 推送文本
     * actionUrl 推送链接看下地址
     */
    public void sendRobotMsg(DingBundleRobotMsgKeyEnum msgKeyEnum, String corpid, String userName, Long dataId, List<String> openConversationIdIn, String text, String actionUrl) {
        String accessToken = null;
        try {
            accessToken = authHelper.getAccessToken(corpid);
        } catch (OApiException e) {
            LOG.error("获取群推送accessToken错误",e);
            return;
        }
        if ( Objects.isNull(accessToken) ){
            LOG.error("获取群推送accessToken为空");
            return;
        }
        String dingtalkUrl = "https://oapi.dingtalk.com/topapi/robot/org/intelligent/message/send?access_token="+accessToken;
        switch (msgKeyEnum){
            case ACTIONCARD_1:
                JSONObject actionParam1 = new JSONObject();
                actionParam1.put("msg_key",msgKeyEnum.getMsgKey());
                JSONObject msgParam1 = new JSONObject();
                LOG.info(actionUrl);
                msgParam1.put("#actionURL#",actionUrl);
                msgParam1.put("#actionTitle#","查看详情");
                msgParam1.put("#text#",text);
                msgParam1.put("#title#","销帮帮CRM");//会话列表中，last message 展示的时候用到
                actionParam1.put("msg_param", msgParam1.toJSONString());
                openConversationIdIn.forEach(openConversationId->{
                    actionParam1.put("open_conversation_id",openConversationId);
                    try {
                        HttpHelper.httpPostDingtalk(dingtalkUrl,actionParam1);
                    } catch (OApiException e) {
                        LOG.error("使用模版1发送群消息出错，错误原因如下:",e);
                    }
                });
                break;
            case ACTIONCARD_2:
                JSONObject actionParam2 = new JSONObject();
                actionParam2.put("msg_key",msgKeyEnum.getMsgKey());
                JSONObject msgParam2 = new JSONObject();
                msgParam2.put("#actionURL1#","");
                msgParam2.put("#actionTitle1#","");
                msgParam2.put("#actionURL2#","");
                msgParam2.put("#actionTitle2#","");
                msgParam2.put("#text#","");
                msgParam2.put("#title#","");
                actionParam2.put("msg_param", msgParam2.toJSONString());
                openConversationIdIn.forEach(openConversationId->{
                    actionParam2.put("open_conversation_id",openConversationId);
                    try {
                        HttpHelper.httpPostDingtalk(dingtalkUrl,actionParam2);
                    } catch (OApiException e) {
                        LOG.error("使用模版2发送群消息出错，错误原因如下:",e);
                    }
                });
                break;
            default:
                break;
        }
    }
    
}
